var currentImage;
var imageCount;
var baseURL='/images/';
var rotate=true;
var rotateQuantum='3000';
var preload_count = 0;

function kaiga_start(image) {
	imageCount=kaiga_picture.length;
	kaiga_showPreview();
	kaiga_refreshThumbnails();
	kaiga_refreshControls();

	if (rotate){
		kaiga_startRotate();
	}

	if (imageCount > 0)
	{
		$("#fancybox_link").fancybox();
	}
	/*$('#kaiga_imageViewContainer').dragscroll();
	$('#kaiga_thumbnailsContainer').dragscroll();
	$('#kaiga_currentImageSubContainer').dragscroll();*/
}

function kaiga_refreshThumbnails(){
	var thumbSrc=document.getElementById('kaiga_thumbnail');
	var thumbCont=document.getElementById('kaiga_thumbnailsContainer');
	
	for (p=0;p<imageCount;p++){
		var thumbTemp=kaiga_copyNode(thumbSrc);
		thumbTemp.style.display='block';
		kaiga_domPrepareThumb(thumbTemp,p+1);
//		kaiga_domSumIdNumber(thumbTemp);
		thumbCont.appendChild(thumbTemp);
	}
}

function kaiga_copyNode(dom){
	var newDom=dom.cloneNode(true);
	
/*	if (dom.childNodes.length){
		for (var n=0;n<dom.childNodes.length;n++){
			newDom.appendChild(kaiga_copyNode(dom.childNodes[n]));
		}
	}
*/	
	return newDom;
}

function kaiga_domPrepareThumb(dom,id,type){
	var imgDom=kaiga_byId(dom,'kaiga_thumbnailImage');
	
	var funcType='preview';
	if (type=='p'||type=='Preview'||type=='preview'){
		funcType='preview';
	}
	if (type=='i'||type=='f'||type=='full'||type=='Image'){
		funcType='image';
	}
	
	var index=id-1;
	imgDom.src = baseURL + kaiga_picture[index].thumbnail.url;

	$(imgDom).click(function() {
		kaiga_stopRotate();
		kaiga_showPreview(id,funcType);
		kaiga_refreshControls();
	});
}

function kaiga_byId(dom,id){
	var matchDom;
	if(dom.childNodes&&dom.childNodes.length){
		for (n=0;n<dom.childNodes.length;n++){
			if (dom.childNodes[n].id==id){
				return dom.childNodes[n];
			}else{
				if (matchDom=kaiga_byId(dom.childNodes[n])){
					return matchDom;
				}else{
					continue;
				}
			}
		}
	}
	
	return false;
}

function kaiga_domSumIdNumber(dom){
	var currentId=false;
	if (dom.id!=''){
		var re = new RegExp('_[0-9]+(?:\.[0-9]*)?$');
		var match = re.exec(dom.id);

		if (match==null){
			currentId=1;
		}else{
			var currentId;
			for (c=1;c<match.length;c++){
				currentId+=match[c];
			}
			currentId=parseInt(currentId);
		}
	}
	
	if (currentId){
		var newId=currentId+1;
		var re = new RegExp('_[0-9]+(?:\.[0-9]*)?$','g');
		var newIdStr=dom.id;
		dom.id=newIdStr.replace(re,'_'+newId);
	}
	
	for (n=0;n<dom.childNodes.lenght;n++){
		domSumIdNumber(dom.childNodes[n]);
	}
}

function kaiga_refreshControls(){
	kaiga_replaceHtml('kaiga_currentImageCount',currentImage);
	kaiga_replaceHtml('kaiga_totalImageCount',imageCount);
	var switchBtn=document.getElementById('kaiga_rotatePicBtn');
	if (rotate){
		switchBtn.src="/kaiga.lib/kaiga_pause.gif";
	}else{
		switchBtn.src="/kaiga.lib/kaiga_play.gif";
	}
}

function kaiga_replaceHtml(domId,html){
	document.getElementById(domId).innerHTML=html;
}

function kaiga_showPreview(index,type){
	if (!type){
		type='preview';
	}
	
	if (isNaN(index)){
		currentImage=1;
	}else{
		currentImage=index;
	}
	
	$("#kaiga_currentImage").attr('src', baseURL + kaiga_picture[currentImage-1][type].url);
	$("#fancybox_link").attr('href', baseURL + kaiga_picture[currentImage-1][type].url);
}

function kaiga_nextPicture(){
	if (rotate){
		kaiga_stopRotate();
	}
	kaiga_nextPictureCB();
}

function kaiga_prevPicture(){
	if (rotate){
		kaiga_stopRotate();
	}
	kaiga_prevPictureCB();
}

function kaiga_nextPictureCB(){
	var nextImage;
	if (currentImage==imageCount){
		nextImage=1;
	}else{
		nextImage=currentImage+1;
	}
	kaiga_showPreview(nextImage);
	kaiga_refreshControls();
}

function kaiga_prevPictureCB(){
	var prevImage;
	if (currentImage==1){
		prevImage=imageCount;
	}else{
		prevImage=currentImage-1;
	}
	kaiga_showPreview(prevImage);
	kaiga_refreshControls();
}

function kaiga_startRotate(){
	rotate=true;
	if (rotateQuantum<=0){
		return true;
	}else{
		timerRotate=setTimeout("kaiga_nextPictureCB();kaiga_startRotate(true);",rotateQuantum);
	}
}

function kaiga_stopRotate(){
	clearTimeout(timerRotate);
	rotate=false;
}

function kaiga_switchRotate(){
	if (rotate){
		kaiga_stopRotate();
	}else{
		kaiga_startRotate();
	}
	
	kaiga_refreshControls();
}

function kaiga_viewPicture(){
	var domContainer	=	document.getElementById('kaiga_imageViewUniverse');
	var domImage		=	document.getElementById('kaiga_image');
	var domHider		=	document.getElementById('kaiga_hider');

		
	domImage.src='';
	domHider.style.display='block';
	domContainer.style.display='block';
	
	domImage.src=baseURL+kaiga_picture[currentImage-1].image.url;
	if (domImage.width>690){
		domImage.width=690;
	}	
}

function kaiga_hidePicture(){
	var domContainer	=	document.getElementById('kaiga_imageViewUniverse');
	var domImage		=	document.getElementById('kaiga_image');
	var domHider		=	document.getElementById('kaiga_hider');
	
	domImage.src='';
	domContainer.style.display='none';
	domHider.style.display='none';
}
